package com.hh.TreeQuestions;

import java.util.*;

/**
 * @Author hanghao
 * @Date 2023/7/3 11:39
 * @Version 1.0
 * 二叉树的层序遍历
 * middle
 */

public class No102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        if (root == null) return list;
        Queue<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int len = deque.size();
            List<Integer> list1 = new ArrayList<>();
            while (len > 0) {
                TreeNode treeNode = deque.poll();
                list1.add(treeNode.val);
                if (treeNode.left != null) {
                    deque.offer(treeNode.left);
                }
                if (treeNode.right != null) {
                    deque.offer(treeNode.right);
                }
                len--;
            }
            list.add(list1);
        }
        return list;
    }
}
